// 导入axios

import axios, { AxiosRequestConfig } from "axios";
// 设置请求的基础域名
axios.defaults.baseURL = "https://m.maizuo.com";
import { Toast } from "vant";
import "vant/es/toast/style";
// 添加请求拦截器
axios.interceptors.request.use(
  (config: AxiosRequestConfig): AxiosRequestConfig => {
    let host = "mall.film-ticket.film.list";
    // 根据不同的路由状态设置 info
    const info = config.headers && config.headers.info;
    if (info === "user") {
      return config;
    }
    if (info === "cinema") {
      host = "mall.film-ticket.cinema.list";
    }
    if (info === "info") {
      host = "mall.film-ticket.film.info";
    }
    if (info === "city") {
      host = "mall.film-ticket.city.list";
    }
    if (info === "news") {
      host = "mall.act.static-page.info";
    }
    // .....后续如果有其他的数据请求需求，就接着写
    if (config.headers && config.headers.authorization) {
      config.headers = {
        authorization: config.headers.authorization,
      };
    } else {
      config.headers = {
        "X-Client-Info":
          '{"a":"3000","ch":"1002","v":"5.0.4","e":"16006566672048699400193","bc":"110100"}',
        "X-Host": host,
      };
    }
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    Toast("网络请求错误，请检查网络！");
    return Promise.reject(error);
  }
);
axios.interceptors.response.use(
  (res) => {
    if (res.data.msg === "ok") {
      Toast("成功返回数据！");
      return res.data;
    } else {
      Toast("数据错误反馈，请检查输入！");
    }
  },
  (err) => {
    console.log(err);
    Toast("网络请求错误，请检查网络！");
  }
);

(async () => {
  const res = await axios.get(
    "gateway?cityId=110100&pageNum=1&pageSize=10&type=1&k=8591124"
  );
  console.log(res);
})();
export default axios;
